/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.sudoku.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.gonalge.sudoku.type.ConfigType;

/**
 * 
 * @author Geremias Gonzalez
 */
public class Configuration {

    private static final String CONFIGURATION_FILE = "config/application.properties";

    private static Properties PROPERTIES;

    private static String getProperty(final String param) {
        if (PROPERTIES == null) {
            loadPropertyFile();
        }
        return PROPERTIES.getProperty(param);
    }

    private static void loadPropertyFile() {
        PROPERTIES = new Properties();
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(CONFIGURATION_FILE);
            PROPERTIES.load(inputStream);
            inputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("Error occurred reading the property " + e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static String getString(final ConfigType key) {
        return getProperty(key.getValue());
    }

    public static Integer getInt(final ConfigType key) {
        return Integer.parseInt(getProperty(key.getValue()));
    }

    public static boolean getBoolean(final ConfigType key) {
        return Boolean.valueOf(getProperty(key.getValue()));
    }

    /**
     * separate id comma ','
     */
    public static String[] getArray(final ConfigType key) {
        return getProperty(key.getValue()).split(",");
    }

}
